package org.jarraiders.musiclibrary.client.presenter;

import java.util.ArrayList;
import java.util.List;

import org.jarraiders.musiclibrary.client.TracksServiceAsync;
import org.jarraiders.musiclibrary.client.event.AddTrackEvent;
import org.jarraiders.musiclibrary.client.event.EditTrackEvent;
import org.jarraiders.musiclibrary.shared.domain.TrackDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class TracksPresenter implements Presenter {

	private List<TrackDTO> trackDetails;

	public interface Display {
		HasClickHandlers getAddButton();

		HasClickHandlers getDeleteButton();

		HasClickHandlers getList();

		HasClickHandlers getSearchButton();

		String getSearchText();

		void setData(List<String> data);

		int getClickedRow(ClickEvent event);

		List<Integer> getSelectedRows();

		Widget asWidget();
	}

	private final TracksServiceAsync rpcService;
	private final HandlerManager eventBus;
	private final Display display;

	public TracksPresenter(TracksServiceAsync rpcService, HandlerManager eventBus, Display view) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = view;
	}

	public void bind() {
		display.getAddButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new AddTrackEvent());
			}
		});

		display.getDeleteButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				//deleteSelectedTracks();
			}
		});

		display.getList().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int selectedRow = display.getClickedRow(event);

				if (selectedRow >= 0) {
					String id = trackDetails.get(selectedRow).getTrackId();
					eventBus.fireEvent(new EditTrackEvent(id));
				}
			}
		});
		
		display.getSearchButton().addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				searchTracks();
				
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		bind();
		container.clear();
		container.add(display.asWidget());
		fetchTrackDetails();

	}

	public void sortTrackDetails() {

		// Yes, we could use a more optimized method of sorting, but the
		// point is to create a test case that helps illustrate the higher
		// level concepts used when creating MVP-based applications.
		//
		for (int i = 0; i < trackDetails.size(); ++i) {
			for (int j = 0; j < trackDetails.size() - 1; ++j) {
				if (trackDetails.get(j).getTrackTitle().compareToIgnoreCase(trackDetails.get(j + 1).getTrackTitle()) >= 0) {
					TrackDTO tmp = trackDetails.get(j);
					trackDetails.set(j, trackDetails.get(j + 1));
					trackDetails.set(j + 1, tmp);
				}
			}
		}
	}

	public void setTrackDetails(List<TrackDTO> contactDetails) {
		this.trackDetails = contactDetails;
	}

	public TrackDTO getContactDetail(int index) {
		return trackDetails.get(index);
	}

	private void fetchTrackDetails() {
		rpcService.getTrackDetails(new AsyncCallback<ArrayList<TrackDTO>>() {
			public void onSuccess(ArrayList<TrackDTO> result) {
				trackDetails = result;
				sortTrackDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(trackDetails.get(i).getTrackTitle());
				}

				display.setData(data);
			}

			public void onFailure(Throwable caught) {
				Window.alert("Error fetching contact details");
			}
		});
	}

	/*private void deleteSelectedTracks() {
		List<Integer> selectedRows = display.getSelectedRows();
		ArrayList<String> ids = new ArrayList<String>();

		for (int i = 0; i < selectedRows.size(); ++i) {
			ids.add(trackDetails.get(selectedRows.get(i)).getTrackId());
		}

		rpcService.deleteTracks(ids, new AsyncCallback<ArrayList<TrackDTO>>() {
			public void onSuccess(ArrayList<TrackDTO> result) {
				trackDetails = result;
				sortTrackDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(trackDetails.get(i).getTrackTitle());
				}

				display.setData(data);

			}

			public void onFailure(Throwable caught) {
				Window.alert("Error deleting selected contacts");
			}
		});
	}
	*/
	private void searchTracks(){
		rpcService.searchTrack(display.getSearchText(), new AsyncCallback<ArrayList<TrackDTO>>() {
			
			@Override
			public void onSuccess(ArrayList<TrackDTO> result) {
				trackDetails = result;
				sortTrackDetails();
				List<String> data = new ArrayList<String>();

				for (int i = 0; i < result.size(); ++i) {
					data.add(trackDetails.get(i).getTrackTitle());
				}

				display.setData(data);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

}
